function listenChat(http){
    var io = require('socket.io')(http);
    var onlineUsers = {};
    var onlineCount = 0;
    io.on('connection',function(socket){
        console.log('listening.....');
        socket.on('login',function(obj){
            socket.name = obj.userId;

            if(!onlineUsers.hasOwnProperty(obj.userId)) {
                onlineUsers[obj.userId] = obj;
                //在线人数+1
                onlineCount++;
            }

            io.emit('login', {onlineUsers:onlineUsers, onlineCount:onlineCount, user:obj});
            console.log(obj.username+'加入了聊天室');
        });
        socket.on('msgSend', function(obj){
            //向所有客户端广播发布的消息
            console.log('get message',obj);
            io.emit('msgSend', obj);
        });
    });

    io.on('disconnect', function(){
        //将退出的用户从在线列表中删除
        if(onlineUsers.hasOwnProperty(socket.name)) {
            //退出用户的信息
            var obj = {userid:socket.name, username:onlineUsers[socket.name]};

            //删除
            delete onlineUsers[socket.name];
            //在线人数-1
            onlineCount--;

            //向所有客户端广播用户退出
            io.emit('logout', {onlineUsers:onlineUsers, onlineCount:onlineCount, user:obj});
        }
    });



}
exports.ioListen = listenChat;